package dgo;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Random;
import dgo.tilemap.MapCharacter;
import javax.microedition.media.Player;
import javax.microedition.media.Manager;
import javax.microedition.media.control.VolumeControl;
import java.io.InputStream;

public class MyLib {
  private static Player[] sounds =new Player[5];// Sounds
  private static int volume = 100;// Volume

  public static int rand(int size){
       Random rand = new Random();
      return Math.abs(rand.nextInt()) % size;
  }
  
  public static byte extractDirX(int dir){
    byte t = 0;
    switch(dir){
      case(1): t = 1; break;
      case(3): t = -1; break;
    }
    return t;
  }
  
 public static byte extractDirY(int dir){
    byte t = 0;
    switch(dir){
      case(0): t = -1; break;
      case(2): t = 1; break;
    }
    return t;
  }
 
 /*
  public static String[] split( String in, char ch ){
      String[] result = new String[2];
      int      pos = in.indexOf( ch );

      if( pos != -1 ){
          result[0] = in.substring( 0, pos ).trim();
          result[1] = in.substring( pos+1 ).trim();
      } else {
          result[0] = in.trim();
      }

      return result;
  }
  * */
  public static void sortCharacters(Vector characters){
    // Bubble sort algorythm
    for(int i = 0; i < characters.size() - 1; i++){

      // Compare bottom up
      for(int j = characters.size() - 1; j > i; j--){
        MapCharacter obj1 = ((MapCharacter)characters.elementAt(j));
        MapCharacter obj2 = ((MapCharacter)characters.elementAt(j - 1));
        if(obj1.mapY < obj2.mapY){
          MapCharacter t = obj1;
          characters.setElementAt(obj2, j);
          characters.setElementAt(obj1, j - 1);
        }
      }
    }
  }
  public static String[] split(String original, String separator) {
    Vector nodes = new Vector();

    // Parse nodes into vector
    int index = original.indexOf(separator);
    while(index>=0) {
      nodes.addElement( original.substring(0, index) );
      original = original.substring(index+separator.length());
      index = original.indexOf(separator);
    }
    // Get the last node
    nodes.addElement( original );

    // Create splitted string array
    String[] result = new String[ nodes.size() ];
    if( nodes.size() > 0) {
      for(int loop=0; loop<nodes.size(); loop++)
      {
        result[loop] = (String)nodes.elementAt(loop);
      }
    }

    return result;
  }
  
  public static Vector clone(Vector v){
    Vector t = new Vector();

    Enumeration e = v.elements();
    while(e.hasMoreElements()){
      t.addElement(e.nextElement());
    }
    
    return t;
  }
  
    //サウンド読み込み
    private static Player readSound(Class cls, String type, String name) 
      throws Exception {
      Player sound=null;
      InputStream in   =null;
      try {
          in = cls.getResourceAsStream(name);
          sound=Manager.createPlayer(in, type);
          sound.prefetch();
          return sound;
      } catch (Exception e) {
          System.out.println(e.toString());
          try {
              if (in!=null) in.close();
          } catch (Exception e2) {
          }
          throw e;
      }
    }
    
    //サウンド再生
    private static void playSound(Class cls, String path, int loopCount, int idx) {
        try{
          if(sounds[idx] != null){sounds[idx].stop();sounds[idx].close();}
          sounds[idx] = readSound(cls, "audio/x-wav", path);
        } catch (Exception e) {
          System.out.println("play sound error: " + e);
        }
        //System.out.println("State:"+sound.getState());
        //stopSound(idx);
        try {
            //Set Volume
            VolumeControl vc=(VolumeControl)
            sounds[idx].getControl("VolumeControl");
            vc.setLevel(volume);

            //Play
            sounds[idx].setLoopCount(loopCount);
            sounds[idx].setMediaTime(0);
            sounds[idx].start();
        } catch (Exception e) {
        }
    }

    public static void playBGM(Class cls, String path) {
      playSound(cls, "/sounds/bgm/" + path + ".wav", -1, 0);
    }
    
    public static void playSE(Class cls, String path) {
      playSound(cls, "/sounds/se/" + path + ".wav", 1, 1);
    }
    
    public static void stopBGM() {
        try {
            sounds[0].stop();
        } catch (Exception e) {
        }
    }
}
